package com.wyw.demo.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.Record;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Redis工具类
 *
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2023/2/27 16:51
 */
@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 创建消费组
     */
    public String createGroup(String key, String group){
        return redisTemplate.opsForStream().createGroup(key, group);
    }

    /**
     * 添加Map消息
     */
    public String addMap(String key, Map<String, String> value){
        return redisTemplate.opsForStream().add(key, value).getValue();
    }

    /**
     * 添加Record消息
     */
    public String addRecord(Record<String, Object> record){
        return redisTemplate.opsForStream().add(record).getValue();
    }

    /**
     * 确认消费
     */
    public Long ack(String key, String group, String... recordIds){
        return redisTemplate.opsForStream().acknowledge(key, group, recordIds);
    }

    /**
     * 删除消息。当一个节点的所有消息都被删除，那么该节点会自动销毁
     */
    public Long del(String key, String... recordIds){
        return redisTemplate.opsForStream().delete(key, recordIds);
    }
}
